using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.PlayerLoop;

[Serializable]
[PlayerStateScript(EPlayerState.Jump)]
public class PlayerJumpState : PlayerStateBase
{
    private const float jumpBeginSpeed = 1f;
    private const float jumpSpeed = 2f;
    private const float MAXJumpTime = 0.3f;

    private float jumpTimer;
    private bool isJumping;
    private static readonly int Jump1 = Animator.StringToHash("Jump");

    public override void EnterState()
    {
        stateMachine.playerComponentContex.animator.SetBool(Jump1, true);
        JumpBeigin();
        jumpTimer = 0;
        isJumping = false;
    }

    public override void ExitState()
    {
        jumpTimer = 0;
        isJumping = false;
        stateMachine.playerComponentContex.animator.SetBool(Jump1, false);
    }

    public override void HandleInput(EPlayerInput playerInput, EInputType inputType)
    {
        switch (playerInput)
        {
            case EPlayerInput.Jump:
                if (inputType == EInputType.Holding)
                {
                    if (jumpTimer < MAXJumpTime && !isJumping)
                    {
                        // Debug.Log(stateMachine.playerComponentContex.rigidbody.velocity.y);
                        isJumping = true;
                    }
                    else if(jumpTimer > MAXJumpTime)
                    {
                        stateMachine.SetState(EPlayerState.Fall);
                    }

                    jumpTimer += Time.deltaTime;
                }
                else if (inputType == EInputType.End)
                {
                    stateMachine.SetState(EPlayerState.Fall);
                }
                break;
        }
    }
    
    private void FallMove()
    {
        var velocity = new Vector2(stateMachine.playerInputDirction.x  * stateMachine.fallingMoveSpeed,stateMachine.playerComponentContex.rigidbody.velocity.y);
        stateMachine.playerComponentContex.rigidbody.velocity = velocity;
    }

    public override void FixedUpdate()
    {
        if (isJumping)
        {
            // Debug.Log("Jumping");
            Jump();
        }
        FallMove();
    }

    private void JumpBeigin()
    {
        // Debug.Log("BeginJump");
        // var force = new Vector2(0, jumpBeginSpeed);
        // stateMachine.playerComponentContex.rigidbody.AddForce(force,ForceMode2D.Impulse);
        stateMachine.playerComponentContex.rigidbody.velocity =
            new Vector2(stateMachine.playerComponentContex.rigidbody.velocity.x, jumpBeginSpeed);
    }

    private void Jump()
    {
        // Debug.Log("HoldJump");
        // var force = new Vector2(0, jumpforce);
        // stateMachine.playerComponentContex.rigidbody.AddForce(force,ForceMode2D.Impulse);
        stateMachine.playerComponentContex.rigidbody.velocity =
            new Vector2(stateMachine.playerComponentContex.rigidbody.velocity.x, jumpSpeed);
    }

    public PlayerJumpState(PlayerStateMachine sm) : base(sm)
    {
    }
}
